package cn.fl.ref.account.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.account.service.IFncGroupCompanyService;
import cn.fl.ref.account.transform.FncGroupCompanyVOTransform;
import cn.fl.ref.account.vo.FncGroupCompanyVO;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.ref.account.api.IFncGroupCompanyAccountApiService;
import fl.ref.account.api.IFncGroupCompanyApiService;
import fl.ref.account.condition.FncGroupCompanyAccountQueryCondition;
import fl.ref.account.condition.FncGroupCompanyQueryCondition;
import fl.ref.account.constant.FncGroupCompanyConstants;
import fl.ref.account.dto.FncGroupCompanyDTO;

/**
 * @Description: 集团公司 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get*
 *               批量：batch* 查询集合：search*
 */
@Service
public class FncGroupCompanyServiceImpl implements IFncGroupCompanyService {

	private static final Logger logger = LoggerFactory.getLogger(IFncGroupCompanyService.class);

	@Autowired
	private ISysDictionaryApiService sysDictionaryApi;
	@Autowired
	private IFncGroupCompanyApiService fncGroupCompanyApiService;
	@Autowired
	private IFlowApiService flowApiService;

	@Autowired
	private IFncGroupCompanyAccountApiService fncGroupCompanyAccountApiService;

	/**
	 * 根据字段代码组获取代码值集合
	 * 
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvList(String grpCd) throws BaseException {
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApi
				.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}

	/**
	 * 根据字段代码组获取代码值集合
	 * 
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvDescList(String grpCd) throws BaseException {
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApi
				.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCodeNm());
				keyValue.setValue(sysDictionaryDTO.getCodeDesc());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}

	@Override
	public FncGroupCompanyVO selectById(Long id) throws BaseException {
		return FncGroupCompanyVOTransform.toVO(fncGroupCompanyApiService.selectById(id));
	}

	@Override
	public Long add(FncGroupCompanyVO entity) throws BaseException {
		return this.fncGroupCompanyApiService.add(FncGroupCompanyVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		return this.fncGroupCompanyApiService.removeById(id);
	}

	@Override
	public boolean update(FncGroupCompanyVO entity) throws BaseException {
		return this.fncGroupCompanyApiService.update(FncGroupCompanyVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		Pager pager = this.fncGroupCompanyApiService.searchListPage(page);
		if (pager != null) {
			List<FncGroupCompanyDTO> resultList = (List<FncGroupCompanyDTO>) pager.getResultList();
			if (CollectionUtils.isNotEmpty(resultList)) {
				// 设置各个类型账户数量
				for (FncGroupCompanyDTO fncGroupCompanyDTO : resultList) {

					FncGroupCompanyAccountQueryCondition condition = new FncGroupCompanyAccountQueryCondition();
					condition.setGroupCompanyId(fncGroupCompanyDTO.getId());
					condition.setIsValid(FncGroupCompanyConstants.IS_VALID_YES);
					condition.setIsDel(FncGroupCompanyConstants.IS_DEL_NO);

					List<Map<String, Object>> accountNumList = fncGroupCompanyAccountApiService
							.getAccountByGroup(condition);
					if (CollectionUtils.isNotEmpty(accountNumList)) {

						for (Map<String, Object> accountNum : accountNumList) {

							String bankNature = (String)accountNum.get("BANK_NATURE");
							BigDecimal bankNatureNum =  (BigDecimal)accountNum.get("BANK_NATURE_NUM");
							if(bankNatureNum != null) {
								if (FncGroupCompanyConstants.FNC_ACCOUNT_BANK_NATURE_1.equals(bankNature)) {
									// 一般存款账户
									fncGroupCompanyDTO.setAccountNum1(bankNatureNum.intValue());
								} else if (FncGroupCompanyConstants.FNC_ACCOUNT_BANK_NATURE_2
										.equals(accountNum.get("BANK_NATURE"))) {
									// 基本存款账户
									fncGroupCompanyDTO.setAccountNum2(bankNatureNum.intValue());
								} else if (FncGroupCompanyConstants.FNC_ACCOUNT_BANK_NATURE_3
										.equals(accountNum.get("BANK_NATURE"))) {
									// 临时存款账户
									fncGroupCompanyDTO.setAccountNum3(bankNatureNum.intValue());
								} else if (FncGroupCompanyConstants.FNC_ACCOUNT_BANK_NATURE_4
										.equals(accountNum.get("BANK_NATURE"))) {
									// 专用存款账户
									fncGroupCompanyDTO.setAccountNum4(bankNatureNum.intValue());
								}
							}
						}

					}
				}

			}
		}

		return pager;
	}

	@Override
	public List<FncGroupCompanyVO> selectList(FncGroupCompanyQueryCondition qc) throws BaseException {
		return FncGroupCompanyVOTransform.toVOList(this.fncGroupCompanyApiService.searchList(qc));
	}

}
